package com.trsjh.db.controller;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

import com.trsjh.db.utils.HibernateUtil;

public class Home {
	public static final String LIST_GOODTYPES="select g from GoodsType g ";
	public static final String LIST_GOODS="select g from Good g";
	public static final String LIST_GOOD_BANDS="select g.brand from Good g";

	public static final String GET_CUSTOMERS="select c from CustomerAccount c";
	public static final String GET_CUSTOMERS_BY_PHONE="select c from CustomerAccount c where phone=?1";
	public static final String GET_CUSTOMERS_BY_NAME="select c from CustomerAccount c where c.name=?1 ";
	
	public static final String GET_CUSTOMER_BY_PHONE_AND_NAME="select c from CustomerAccount c where c.customer.phone=?1 and c.name=?2";
	public static final String LIST_CUSTOMER_RECORDS="select cr from CustomerRecord cr where cr.customerAccount=?1";
	public static final String LIST_GOODS5="select g from Good g";
	public static final String LIST_GOODS6="select g from Good g";

	private static final Log log = LogFactory.getLog(Home.class);
//	{
//		PropertyConfigurator.configure(Home.class.getResource( "/conf/log4j.properties") );
//		log.debug ("D") ; 
//		log.info ( "D") ; 
//		log.warn ("D") ; 
//		log.error ("D") ;	
//		log.error ("stdout") ;
//	}
	
	EntityManager entityManager ;
	public Home(){
		this.entityManager=HibernateUtil.createEntityManager();
	}
	public Home(EntityManager entityManager ){
		this.entityManager=entityManager;
	}
	public void closeEntityManager(){
		if(this.entityManager!=null&&this.entityManager.isOpen())
			this.entityManager.close();
	}
	public void beginTransaction(){
		this.entityManager.getTransaction().begin();
	}
	public void commit(){
		try{
			this.entityManager.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			this.entityManager.getTransaction().rollback();
		}
	}
	public void rollBack(){
		this.entityManager.getTransaction().rollback();
	}
	public void persist(Object transientInstance) {
		log.debug("persisting  instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
		}
		catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Object persistentInstance) {
		log.debug("removing object instance");
		try {
			entityManager.remove(persistentInstance);
			log.debug("remove successful");
		}
		catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Object merge(Object detachedInstance) {
		log.debug("merging Object instance");
		try {
			Object result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		}
		catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Object findById( Integer id) {
		log.debug("getting GoodsType instance with id: " + id);
		try {
			Object instance = entityManager.find(Object.class, id);
			log.debug("get successful");
			return instance;
		}
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	@SuppressWarnings("rawtypes")
	public List list(String str,Map<Integer,Object> params){
		log.debug("list:"+str);
		try {
			Query q=entityManager.createQuery(str);
			if(params!=null)
				for(Integer i:params.keySet()){
					q.setParameter(i, params.get(i));
				}
			List list=q.getResultList();
			log.debug("get successful");
			return list;
		}
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public Object getFirst(String query,Map<Integer,Object> params){
		log.debug("list:"+query);
		try {
			Query q=entityManager.createQuery(query);
			if(params!=null)
				for(Integer i:params.keySet()){
					q.setParameter(i, params.get(i));
				}
			@SuppressWarnings("rawtypes")
			List list=q.getResultList();
			log.debug("get successful");
			if(list.size()>0){
				return list.get(0);
			}else{
				return null;
			}
		}
		catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	@Override
	protected void finalize() throws Throwable {
		if(this.entityManager.isOpen()){
			this.entityManager.close();
		}
		super.finalize();
	}
	
}
